﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.CRM.Library;
using ZX.BLL.Extend;
using ZX.DAL.DBExtend;
using System.Data;

namespace ZX.CRM.Bussiness
{
    /// <summary>
    /// 订单业务处理类
    /// </summary>
    public class BTCrmCrustomerOrder : BaseBussnies
    {
        /// <summary>
        /// 获取所有产品列表
        /// </summary>
        /// <param name="TypeId">产品类型</param>
        /// <returns></returns>
        public IList<V_CrmProduct> GetAllCrmCustomerProduct(string TypeId)
        {
            this.entity = new V_CrmProduct();
            if (TypeId == "")
            {
                return GetEntitysBySqlComm<V_CrmProduct>("select * from V_CrmProduct order by Caption");
            }
            else
            {
                return GetEntitysBySqlComm<V_CrmProduct>("select * from V_CrmProduct where TypeId='" + TypeId + "' order by Caption");
            }
        }
        /// <summary>
        /// 根据ID获取订单信息
        /// </summary>
        public V_CrmCustomerOrder GetAllCrmCustomerOrder(Guid OrderId)
        { 
            this.entity = new V_CrmCustomerOrder();
            return this.GetEntityBySqlComm("select * from V_CrmCustomerOrder where ID='" + OrderId + "'") as V_CrmCustomerOrder;
        }
        /// <summary>
        /// 根据流程ID
        /// </summary>
        /// <param name="WorkFlowInsId"></param>
        /// <returns></returns>
        public V_CrmCustomerOrder GetAllCrmCustomerOrderByWorkFlowInsId(Guid WorkFlowInsId)
        {
            this.entity = new V_CrmCustomerOrder();
            return this.GetEntityBySqlComm("select * from V_CrmCustomerOrder where WorkFlowInsId='" + WorkFlowInsId + "'") as V_CrmCustomerOrder;
        }
        /// <summary>
        /// 根据ID获取订单信息
        /// </summary>
        public V_CrmCustomerInfoOrder GetAllCrmCustomerInfoOrder(Guid OrderId)
        {
            this.entity = new V_CrmCustomerInfoOrder();
            return this.GetEntityBySqlComm("select * from V_CrmCustomerInfoOrder where ID='" + OrderId + "'") as V_CrmCustomerInfoOrder;
        }
        /// <summary>
        /// 根据客户ID获取销售订单信息(类型不为约场,状态没有结案)
        /// </summary>
        /// <param name="CustomerId">客户ID</param>
        /// <returns></returns>
        public IList<V_CrmCustomerInfoDeliver> GetAllCrmCustomerInfoOrderByCustomerId(Guid CustomerId)
        {
            this.entity = new V_CrmCustomerInfoDeliver();
            return GetEntitysBySqlComm<V_CrmCustomerInfoDeliver>("select distinct   ID, Code, CustomerId, DeptId, Date, TypeID, ValutaID, PillID, CourseLoyoutID, TotalMoney, ResignMoney, PaymentId, Explain, AddUser, AddTime, " +
                      "EXP1, EXP2, EXP3, EXP4, DeptName, OrderType, ValuteType, PaymentType, StateID, OrderState, UName, OrderResignMoney, WorkFlowId, " +
                      "WorkFlowInsId, WorkTaskId, WorkTaskInsId, Expr1, CustomerName, CompanyCreateDate, CompanyProduct, Expr2, LevelName, OriginName, " +
                      "TypeName, TradeName, ProductName, PersonName, AddUserName, SaleadviserName, LectorName, CustomerMgr, DetailAddress " +
                      "from V_CrmCustomerInfoDeliver where CustomerId='" + CustomerId + "' and TypeID<>'5E50217A-414E-46D5-89BC-348AD0145FFC' " +
                      "and StateID<>'BDDCAF31-98D4-423A-9275-9288F85E2847'");
        }
        /// <summary>
        /// 根据客户ID,客户编码获取销售订单信息(类型不为约场,状态没有结案)
        /// </summary>
        /// <param name="CustomerId">客户ID</param>
        /// <param name="strCode">客户编码</param>
        /// <returns></returns>
        public IList<V_CrmCustomerInfoDeliver> GetAllCrmCustomerInfoOrderByCustomerId(Guid CustomerId, string strCode)
        {
            this.entity = new V_CrmCustomerInfoDeliver();
            return GetEntitysBySqlComm<V_CrmCustomerInfoDeliver>("select distinct   ID, Code, CustomerId, DeptId, Date, TypeID, ValutaID, PillID, CourseLoyoutID, TotalMoney, ResignMoney, PaymentId, Explain, AddUser, AddTime, " +
                      "EXP1, EXP2, EXP3, EXP4, DeptName, OrderType, ValuteType, PaymentType, StateID, OrderState, UName, OrderResignMoney, WorkFlowId, " +
                      "WorkFlowInsId, WorkTaskId, WorkTaskInsId, Expr1, CustomerName, CompanyCreateDate, CompanyProduct, Expr2, LevelName, OriginName, " +
                      "TypeName, TradeName, ProductName, PersonName, AddUserName, SaleadviserName, LectorName, CustomerMgr, DetailAddress " +
                      "from V_CrmCustomerInfoDeliver where CustomerId='" + CustomerId + "' and TypeID<>'5E50217A-414E-46D5-89BC-348AD0145FFC' " +
                      "and StateID<>'BDDCAF31-98D4-423A-9275-9288F85E2847' and Code='" + strCode + "'");
        }
        /// <summary>
        /// 根据流程ID
        /// </summary>
        /// <param name="WorkFlowInsId"></param>
        /// <returns></returns>
        public V_CrmCustomerInfoOrder GetAllCrmCustomerInfoOrderByWorkFlowInsId(Guid WorkFlowInsId)
        {
            this.entity = new V_CrmCustomerInfoOrder();
            return this.GetEntityBySqlComm("select * from V_CrmCustomerInfoOrder where WorkFlowInsId='" + WorkFlowInsId + "'") as V_CrmCustomerInfoOrder;
        }
        /// <summary>
        /// 根据ID获取订单信息
        /// </summary>
        public IList<V_CrmProductPay> GetAllCrmProductPayByCid(Guid CustomerId)
        {
            this.entity = new V_CrmProductPay();
            return GetEntitysBySqlComm<V_CrmProductPay>("select * from V_CrmProductPay where AccountReceivable>0 and CustomerID='" + CustomerId + "' and AccountReceivable<>ResignMoney and orderid in(select id from CrmCustomerOrder where StateID in(select PhraseID from DatPhraseDetail where ClassID =(select ClassID from DatPhraseClass where ClassValue='OrderStateID')and (PhraseValue='4'or PhraseValue='3'))) order by OrderProductId");
        }
        /// <summary>
        /// 根据订单ID获取所有的订单产品信息
        /// </summary>
        /// <param name="OrderID">订单ID</param>
        /// <returns></returns>
        public IList<V_CrmOrderProduct> GetAllOrderProduct(Guid OrderID)
        {
            this.entity = new V_CrmOrderProduct();
            return GetEntitysBySqlComm<V_CrmOrderProduct>("select * from V_CrmOrderProduct where OrderID='" + OrderID + "'");
        }
        public IList<V_CrmOrderProductInfo> GetAllOrderProductListByOrderId(Guid OrderId)
        {
            this.entity = new V_CrmOrderProductInfo();
            return GetEntitysBySqlComm<V_CrmOrderProductInfo>("select * from V_CrmOrderProductInfo where OrderID='" + OrderId + "'");
        }
        /// <summary>
        /// 根据现场销售ID获取所有的现场销售产品信息
        /// </summary>
        /// <param name="OrderID">现场销售ID</param>
        /// <returns></returns>
        public IList<V_CrmLiveOrderProduct> GetAllLiveProduct(Guid LiveID)
        {
            this.entity = new V_CrmLiveOrderProduct();
            return GetEntitysBySqlComm<V_CrmLiveOrderProduct>("select * from V_CrmLiveOrderProduct where LiveID='" + LiveID + "'");
        }
        /// <summary>
        /// 获取订单中所有人员比例所有信息
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public IList<V_CrmOrderPerson> GetAllCrmOrderPerson(Guid OrderId)
        {
            this.entity = new V_CrmOrderPerson();
            return GetEntitysBySqlComm<V_CrmOrderPerson>("select * from V_CrmOrderPerson where OrderId='" + OrderId + "'");
        }
        /// <summary>
        /// 获取订单ID获取助约人信息
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public IList<V_CrmOrderPerson> GetOrderPerson(Guid OrderId,string strClassValue)
        {
            this.entity = new V_CrmOrderPerson();
            return GetEntitysBySqlComm<V_CrmOrderPerson>("select * from V_CrmOrderPerson where OrderId='" + OrderId + "' and ClassValue='" + strClassValue + "'");
        }
        /// <summary>
        /// 获取客户总监人员比例所有信息
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public IList<V_CrmOrderPerson> GetCustomerZjPerson(Guid OrderId)
        {
            this.entity = new V_CrmOrderPerson();
            return GetEntitysBySqlComm<V_CrmOrderPerson>("select * from V_CrmOrderPerson where OrderId='" + OrderId + "' and ClassValue='CustomerQDR'");
        }
        /// <summary>
        /// 检查客户是否已下过约场订单
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        public IList<CrmCustomerOrder> CheckCustomerOrder(Guid CustomerId)
        {
            this.entity = new CrmCustomerOrder();
            return GetEntitysBySqlComm<CrmCustomerOrder>("select * from CrmCustomerOrder where CustomerId='" + CustomerId + "' and TypeID='5E50217A-414E-46D5-89BC-348AD0145FFC'");
        }
        /// <summary>
        /// 获取所有缴款ID
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        public IList<CrmPay> GetAllCrmPayIdByCustomerId(Guid CustomerId)
        {
            this.entity = new CrmPay();
            return GetEntitysBySqlComm<CrmPay>("select ID,Code from CrmPay where CustomerId='" + CustomerId + "'");
        }
        /// <summary>
        /// 获取分公司
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public DataTable GetBranchCompany(Guid userId)
        {
            DataTable dt = new DataTable();
            try
            {
                DbSql db = new DbSql();
                string strSql = string.Format("select * from fn_GetDepartNameAndIDByUserID('{0}')", userId);
                dt = db.ExecuteQuery(strSql);
            }
            catch
            {
                new DataTable();
            }
            return dt;
        }
        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetDeptByUserId(Guid userId)
        {
            DataTable dt = new DataTable();
            try
            {
                DbSql db = new DbSql();
                string strSql = string.Format("select * from fn_GetDeptInfoByUserId('{0}')", userId);
                dt = db.ExecuteQuery(strSql);
            }
            catch
            {
                new DataTable();
            }
            return dt;
        }
    }
}
